package data;

import helpers.Configuration;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//Singleton Class

public class DBRegistry
{

    private static DBRegistry currentInstance=null;
	private static Connection connection = null;

    //-----------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------

    private DBRegistry() //private default constructor
    {

    }

    //-----------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------

    public synchronized static DBRegistry createInstance()
    {
        if(currentInstance==null)
        {
            currentInstance = new DBRegistry();
            try
            {
            	Class.forName("org.sqlite.JDBC");
	            connection = DriverManager.getConnection("jdbc:sqlite:" + Configuration.getAttribute("dbname"));
            } catch (Exception e)
            {
	            e.printStackTrace();
            }
        }

        return currentInstance;
    }

    //-----------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------

    public Connection getDBConnection()
    {
    	return connection;
    }

    //-----------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------
    
    public void openDBConnection()
    {
		try
		{
			Class.forName("org.sqlite.JDBC");
			connection = DriverManager.getConnection("jdbc:sqlite:" + Configuration.getAttribute("dbname"));
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
    }
    
    //-----------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------

    public void closeDBConnection()
    {
    	try
    	{
    		connection.close();
    		connection = null;
    	}
    	
    	catch(Exception g)
    	{
    		
    	}
    }
    
    //-----------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------
	/**
	 * Executes sql on selected database
	 * This is synchronized to ensure atomic access.
	 * @param sql the sql statement to be processed
	 */
	public synchronized ResultSet execute(String sql)
	{
		ResultSet rs = null; 
		Statement statement;
		
		try
		{
			statement = createInstance().getDBConnection().createStatement();	
			rs = statement.executeQuery(sql);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		return rs; 
	}

	public void exupdate(String str)
	{
		Statement statement;
		try
		{
			statement = connection.createStatement();	
			statement.executeUpdate(str);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
    //-----------------------------------------------------------------------------------------------
    //-----------------------------------------------------------------------------------------------

}
